package hello.world;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

public class Sprite
{
    private int [] textureID = null;
    private float2 position = new float2(0.0f, 0.0f);
    private float rotation = 0.0f;
    private int2 size = new int2(1, 10);
    private float [] color = new float[4];   // RGBA
    private static final float [] vertices =
    {
        -1.0f, -1.0f,
        -1.0f, 1.0f,
        1.0f, -1.0f,
        1.0f, 1.0f
    };
    private static final float [] texCoords =
    {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 0.0f,
            1.0f, 1.0f
    };
    private static FloatBuffer verticesBuffer = null;
    private static FloatBuffer texCoordsBuffer = null;
    
    public Sprite()
    {
        if (verticesBuffer == null)
        {
            ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); 
            vbb.order(ByteOrder.nativeOrder());
            verticesBuffer = vbb.asFloatBuffer();
            verticesBuffer.put(vertices);
            verticesBuffer.position(0);
        }
        if (texCoordsBuffer == null)
        {
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(texCoords.length * 4); 
            byteBuffer.order(ByteOrder.nativeOrder());
            texCoordsBuffer = byteBuffer.asFloatBuffer();
            texCoordsBuffer.put(texCoords);
            texCoordsBuffer.position(0);
        }
        color[0] = 1.0f;
        color[1] = 1.0f;
        color[2] = 1.0f;
        color[3] = 1.0f;
    }
    
    public void loadTexture(int imageResID, GL10 gl, Context context)
    {
        textureID = new int[1];
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageResID);
        size = new int2(bitmap.getWidth(), bitmap.getHeight());
        gl.glGenTextures(1, textureID, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID[0]);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();
    }
    
    public float2 getPosition()
    {
        return position;
    }
    
    public void setPosition(float2 _position)
    {
        position = _position;
    }
    
    public float getRotation()
    {
        return rotation;
    }
    
    public void setRotation(float _rotation)
    {
        rotation = _rotation;
    }
    
    public float [] getColor()
    {
        return color;
    }
    
    public void setColor(float [] _color)
    {
        color = _color;
    }
    
    public float getAlpha()
    {
        return color[3];
    }
    
    public void setAlpha(float _alpha)
    {
        color[3] = _alpha;
    }
    
    public void draw(GL10 gl)
    {
        gl.glLoadIdentity();
        gl.glTranslatef(position.x, position.y, 0.0f);
        gl.glRotatef(rotation, 0.0f, 0.0f, 1.0f);
        gl.glScalef(size.x * 0.5f, size.y * 0.5f, 0.0f);
        
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID[0]);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, verticesBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoordsBuffer);
        gl.glColor4f(color[0], color[1], color[2], color[3]);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    }
}
